package com.nobuyme.box.toy;


import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.nobuyme.box.R;
import com.nobuyme.box.account.AccountManager;
import com.nobuyme.box.app.UrlCenter;
import com.nobuyme.box.bean.GoodsListEvent;
import com.nobuyme.box.common.http.MyHttpRequest;
import com.pachong.android.baseuicomponent.adapter.RecyclerViewBaseAdapter;
import com.pachong.android.baseuicomponent.fragment.SwipeGridFragment;
import com.pachong.android.framework.httprequest.IHttpRequest;
import com.pachong.android.framework.httprequest.UrlParams;
import com.pachong.android.framework.httprequest.responsecallbacks.ListDataRequestListener;
import com.pachong.android.frameworkbase.utils.EasyToast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.Serializable;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 */
public class ToyListFragment extends SwipeGridFragment {
    int id;
    private ToyRentListAdapter toyRentListAdapter;
    public static final int EMITYBOX = 0;
    public static final int WAITRENTBOX = 1;
    public static final int WAITBACKBOX = 2;
    public static final int LOCKBOX = 3;
    private int mPage = 0;
    private int position;

    public ToyListFragment() {
        super(3);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        EventBus.getDefault().register(this);
        clearDecorations();
        setPullToRefreshEnable(false);
        id = getArguments().getInt(ToyListActivity.ID);
        position = getArguments().getInt(ToyListActivity.POSITION);
        startLoading();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long itemId) {
        super.onItemClick(arg0, arg1, position, itemId);
        TestBean data = (TestBean) getData().get(position);
        if (data.getStatus() == EMITYBOX) {
            EasyToast.showToast(getContext(), "暂无玩具");
        } else if (data.getStatus() == WAITRENTBOX) {
            ToyIntroduceActivity.start(getActivity(), data, data.getLattice_code());
        } else if (data.getStatus() == WAITBACKBOX) {
            EasyToast.showToast(getContext(), "等待管理员回收该玩具");
        } else if (data.getStatus() == LOCKBOX) {
            EasyToast.showToast(getContext(), "该玩具已被锁定");
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(GoodsListEvent refreshEvent) {
        getData().clear();
        int mId = refreshEvent.getId();
        int id = AccountManager.getCurrentAccount().getId();
        mPage = 1;
        if (mResponceListener.getResponseDataParser().hasMore()) {
            mPage++;
        }
        IHttpRequest request = new MyHttpRequest(getContext());
        UrlParams params = new UrlParams();
        params.put("categoryId", mId + "");
        params.put("toyboxId", id + "");//toybox 机子对应的id
        params.put("pageNo", mPage + "");
        params.put("pageSize", 80 + "");
        params.put("status", 1 + "");
        request.get(UrlCenter.GOODS_LIST, params, mResponceListener);
        toyRentListAdapter.notifyDataSetChanged();

    }

    @Override
    public RecyclerViewBaseAdapter createAdapter() {
        toyRentListAdapter = new ToyRentListAdapter(getActivity());
        return toyRentListAdapter;
    }

    private ListDataRequestListener<TestBean> mResponceListener = new ListDataRequestListener<>(this, TestBean.class);

    @Override
    public void onStartLoading() {
        int lid = AccountManager.getCurrentAccount().getId();
        if (mResponceListener.getResponseDataParser().hasMore()) {
            mPage++;
        }
        IHttpRequest request = new MyHttpRequest(getContext());
        UrlParams params = new UrlParams();
        params.put("categoryId", id + "");
        params.put("toyboxId", lid + "");//toybox 机子对应的id
        params.put("pageNo", mPage + "");
        params.put("pageSize", 80 + "");
        params.put("status", 1 + "");
        request.get(UrlCenter.GOODS_LIST, params, mResponceListener);
    }

    public class ToyRentListAdapter extends RecyclerViewBaseAdapter<TestBean> {
        private Context con;

        public ToyRentListAdapter(Context con) {
            super(con);
            this.con = con;
        }

        @Override
        public RecyclerView.ViewHolder onCreateHolder(ViewGroup parent, int viewType) {
            return new ToyListHolder(setAdapterViewItem(parent, R.layout.toy_list_detail_item));
        }

        @Override
        public void onBindHolder(RecyclerView.ViewHolder holder, int position) {
            final TestBean data = (TestBean) getData().get(position);
            final ToyListHolder itemHolder = (ToyListHolder) holder;

            if (data.getStatus() == EMITYBOX ) {
                itemHolder.mIvRentDetail.setImageResource(R.mipmap.emity_pic);
                itemHolder.mTvRentName.setText("空储物格");
                itemHolder.mTvRentPrice.setText("-");
            } else {
                Glide.with(con).load(data.getCover()).into(itemHolder.mIvRentDetail);
                itemHolder.mTvRentName.setText(data.getName());
                itemHolder.mTvRentPrice.setText("前"+data.getLeast_rent_days()+"天"+data.getFirst_rent_price()+"元");
            }
            itemHolder.mTvCustomerNum.setText(data.getLattice_code());
            itemHolder.mIvEmpty.setVisibility(View.GONE);
            itemHolder.mIvRetrievey.setVisibility(View.GONE);
            itemHolder.mIvLock.setVisibility(View.GONE);
            if (data.getStatus() == EMITYBOX) {
                Log.e("data.getStatus()", data.getStatus() + "");
                itemHolder.mIvEmpty.setVisibility(View.VISIBLE);
            } else if (data.getStatus() == WAITBACKBOX) {
                itemHolder.mIvRetrievey.setVisibility(View.VISIBLE);
            } else if (data.getStatus() == LOCKBOX) {
                itemHolder.mIvLock.setVisibility(View.VISIBLE);
            }
        }

        class ToyListHolder extends RecyclerView.ViewHolder {
            @Bind(R.id.iv_rent_detail)
            ImageView mIvRentDetail;
            @Bind(R.id.iv_empty)
            ImageView mIvEmpty;
            @Bind(R.id.iv_lock)
            ImageView mIvLock;
            @Bind(R.id.iv_retrievey)
            ImageView mIvRetrievey;
            @Bind(R.id.tv_customer_num)
            TextView mTvCustomerNum;
            @Bind(R.id.tv_rent_name)
            TextView mTvRentName;
            @Bind(R.id.tv_rent_price)
            TextView mTvRentPrice;


            public ToyListHolder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }
    }

    public class TestBean implements Serializable {

        /**
         * goods_id : 1
         * goods_code : 2014
         * toybox_id : 1
         * toybox_code : 测试
         * lattice_id : 1
         * lattice_code : MV00001
         * name : 测试
         * cover : http://ooftwgdmf.bkt.clouddn.com/2ed7ba12-304f-49ac-8560-5b2f91879573.png
         * status : 1
         * status_str : 待租赁
         * lease_price
         */

        private int goods_id;
        private String goods_code;
        private int toybox_id;
        private String toybox_code;
        private int lattice_id;
        private String lattice_code;
        private String name;
        private String cover;
        private int status;
        private String status_str;
        private double lease_price;
        private double first_rent_price;
        private int least_rent_days;

        public int getLeast_rent_days() {
            return least_rent_days;
        }

        public void setLeast_rent_days(int least_rent_days) {
            this.least_rent_days = least_rent_days;
        }



        public double getFirst_rent_price() {
            return first_rent_price;
        }

        public void setFirst_rent_price(double first_rent_price) {
            this.first_rent_price = first_rent_price;
        }

        public double getLease_price() {
            return lease_price;
        }

        public void setLease_price(double lease_price) {
            this.lease_price = lease_price;
        }

        public void setGoods_id(int goods_id) {
            this.goods_id = goods_id;
        }

        public void setGoods_code(String goods_code) {
            this.goods_code = goods_code;
        }

        public void setToybox_id(int toybox_id) {
            this.toybox_id = toybox_id;
        }

        public void setToybox_code(String toybox_code) {
            this.toybox_code = toybox_code;
        }

        public void setLattice_id(int lattice_id) {
            this.lattice_id = lattice_id;
        }

        public void setLattice_code(String lattice_code) {
            this.lattice_code = lattice_code;
        }

        public void setName(String name) {
            this.name = name;
        }

        public void setCover(String cover) {
            this.cover = cover;
        }

        public void setStatus(int status) {
            this.status = status;
        }

        public void setStatus_str(String status_str) {
            this.status_str = status_str;
        }

        public int getGoods_id() {
            return goods_id;
        }

        public String getGoods_code() {
            return goods_code;
        }

        public int getToybox_id() {
            return toybox_id;
        }

        public String getToybox_code() {
            return toybox_code;
        }

        public int getLattice_id() {
            return lattice_id;
        }

        public String getLattice_code() {
            return lattice_code;
        }

        public String getName() {
            return name;
        }

        public String getCover() {
            return cover;
        }

        public int getStatus() {
            return status;
        }

        public String getStatus_str() {
            return status_str;
        }

        @Override
        public String toString() {
            return "TestBean{" +
                    "goods_id=" + goods_id +
                    ", goods_code='" + goods_code + '\'' +
                    ", toybox_id=" + toybox_id +
                    ", toybox_code='" + toybox_code + '\'' +
                    ", lattice_id=" + lattice_id +
                    ", lattice_code='" + lattice_code + '\'' +
                    ", name='" + name + '\'' +
                    ", cover='" + cover + '\'' +
                    ", status=" + status +
                    ", status_str='" + status_str + '\'' +
                    '}';
        }
    }
}
